<?php

namespace App\Admin\Controllers;

use App\Models\GoldShop;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class GoldShopController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('金币小铺管理');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑积分商品');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('创建积分商品');

            $form = $this->form();

            $content->body($form);
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(GoldShop::class, function (Grid $grid) {

            $grid->GoldShopID('ID')->sortable();
            
            $grid->Integral('积分数量');
            $grid->GoldCoin('金币数量');
            $grid->WinningPercentage('中奖几率')->display(function($data){
                return $data."%";
            });
            $grid->JoinNum('预计参与人数');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(GoldShop::class, function (Form $form) {

            $form->hidden('GoldShopID', 'ID');
            
            $form->number('Integral','积分数量');
            $form->number('GoldCoin','金币数量');
            $form->rate('WinningPercentage','中奖几率');
            $form->number('JoinNum','预计参与人数');

        });
    }
}
